package com.example.myapplication;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GameCircleViewModel extends ViewModel {
    // 用于存储和观察游戏列表数据
    private MutableLiveData<List<Game>> mGameListLiveData = new MutableLiveData<>();

    // 线程池，用于模拟网络请求
    private ExecutorService mExecutor = Executors.newSingleThreadExecutor();

    // 获取可观察的数据
    public MutableLiveData<List<Game>> getGameListLiveData() {
        return mGameListLiveData;
    }

    // 加载游戏数据（模拟网络请求）
    public void loadGameData() {
        // 在子线程中执行，模拟网络请求延迟
        mExecutor.execute(() -> {
            try {
                // 模拟网络请求延迟
                Thread.sleep(1000);

                // 创建模拟数据
                List<Game> gameList = new ArrayList<>();
                gameList.add(new Game(1, "英雄联盟", "热门MOBA竞技游戏", R.drawable.game_icon1));
                gameList.add(new Game(2, "王者荣耀", "移动端5v5游戏", R.drawable.game_icon2));
                gameList.add(new Game(3, "原神", "开放世界角色扮演游戏", R.drawable.game_icon3));
                gameList.add(new Game(4, "我的世界", "沙盒建造游戏", R.drawable.game_icon4));
                gameList.add(new Game(5, "和平精英", "战术竞技射击游戏", R.drawable.game_icon5));

                // 切换到主线程更新数据
                mGameListLiveData.postValue(gameList);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    // 删除游戏项
    public void deleteGame(int position) {
        // 获取当前列表
        List<Game> currentList = mGameListLiveData.getValue();
        if (currentList != null && position >= 0 && position < currentList.size()) {
            // 创建新列表（避免直接修改原列表）
            List<Game> newList = new ArrayList<>(currentList);
            newList.remove(position);
            // 更新数据
            mGameListLiveData.setValue(newList);
        }
    }

    // 销毁时释放资源
    @Override
    protected void onCleared() {
        super.onCleared();
        mExecutor.shutdown();
    }
}
